<script setup>
import {
  Management,
  Promotion,
  UserFilled,
  User,
  Crop,
  EditPen,
  SwitchButton,
  CaretBottom
} from '@element-plus/icons-vue'
import avatar from '@/assets/default.png'
import { onMounted } from 'vue'
import { reqGetUserInfo } from '@/api/user';
import { useUserStore } from '@/stores/modules/user';
const userStore = useUserStore() 

import {useRouter} from 'vue-router'
const router = useRouter()

onMounted(async () => {
 let res = await reqGetUserInfo()
  if (res.code === 200) {
    userStore.setUserInfo(res.data)  
  }
})

function exitSystem() {
  userStore.removeToken();
  userStore.removeUserInfo();
  router.push('/login')
}

</script>

<template>
  <el-container class="layout-container">
    <el-aside width="200px">
      <div class="el-aside__logo"></div>
      <el-menu
        active-text-color="#ffd04b"
        background-color="#232323"
        :default-active="$route.path"
        text-color="#fff"
        router
      > 
      <el-menu-item index="/welcome">
          <el-icon><Management /></el-icon>
          <!-- <el-icon><HomeFilled /></el-icon> -->
          <span>首页</span>
        </el-menu-item>

        <el-menu-item index="/user">
          <el-icon><Management /></el-icon>
          <span>用户管理</span>
        </el-menu-item>
        <el-menu-item index="/dept">
          <el-icon><Promotion /></el-icon>
          <span>部门管理</span>
        </el-menu-item>
        <el-sub-menu index="/center">
          <template #title>
            <el-icon><UserFilled /></el-icon>
            <span>个人中心</span>
          </template>
          <el-menu-item index="/user/profile">
            <el-icon><User /></el-icon>
            <span>基本资料</span>
          </el-menu-item>
          <el-menu-item index="/user/avatar">
            <el-icon><Crop /></el-icon>
            <span>更换头像</span>
          </el-menu-item>
          <el-menu-item index="/user/password">
            <el-icon><EditPen /></el-icon>
            <span>重置密码</span>
          </el-menu-item>
        </el-sub-menu>
      </el-menu>
    </el-aside>
    <el-container>
      <el-header>
        <div>当前用户：<strong>{{userStore.userInfo.nickName}}</strong></div>
        <el-dropdown placement="bottom-end">
          <span class="el-dropdown__box">
            <el-avatar :src="userStore.userInfo.avatar || avatar" />
            <el-icon><CaretBottom /></el-icon>
          </span>
          <template #dropdown>
            <el-dropdown-menu>
            
              <el-dropdown-item @click="exitSystem" command="logout" :icon="SwitchButton"
                >退出登录</el-dropdown-item
              >
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </el-header>
      <el-main>
        <router-view></router-view>
      </el-main>
      <el-footer>后台管理系统 ©2023 Created by 鲲鹏产业学院</el-footer>
    </el-container>
  </el-container>
</template>

<style lang="scss" scoped>
.layout-container {
  height: 100vh;
  .el-aside {
    background-color: #232323;
    &__logo {
      height: 120px;
      background: url('@/assets/logo.png') no-repeat center / 120px auto;
    }
    .el-menu {
      border-right: none;
    }
  }
  .el-header {
    background-color: #fff;
    display: flex;
    align-items: center;
    justify-content: space-between;
    .el-dropdown__box {
      display: flex;
      align-items: center;
      .el-icon {
        color: #999;
        margin-left: 10px;
      }

      &:active,
      &:focus {
        outline: none;
      }
    }
  }
  .el-footer {
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 14px;
    color: #666;
  }
}
</style>